package com.crm.miaohe.controller.strategy;

import com.crm.miaohe.common.ApplicationConstant;
import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.strategy.request.MultiPriceStrategyParams;
import com.crm.miaohe.controller.strategy.request.PriceStrategyParams;
import com.crm.miaohe.controller.strategy.request.PriceStrategyQueryParams;
import com.crm.miaohe.controller.strategy.response.PriceStrategyDetail;
import com.crm.miaohe.controller.strategy.response.PriceStrategyItemGroup;
import com.crm.miaohe.exception.ErpBusinessException;
import com.crm.miaohe.model.TblPriceStrategy;
import com.crm.miaohe.model.TblPriceStrategyItem;
import com.crm.miaohe.service.PriceStrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/erp/strategy")
public class PriceStrategyController {

    private final PriceStrategyService priceStrategyService;

    public PriceStrategyController(PriceStrategyService priceStrategyService) {
        this.priceStrategyService = priceStrategyService;
    }

    @GetMapping("/{strategyId}")
    public ResponseEntity<List<PriceStrategyItemGroup>> findStrategy(@PathVariable Long strategyId) {
        return ResponseEntity.ok(priceStrategyService.strategyItems(strategyId));
    }

    @GetMapping("/detail/{strategyId}")
    public ResponseEntity<PriceStrategyDetail> detail(@PathVariable Long strategyId) {
        return ResponseEntity.ok(priceStrategyService.getStrategyDetail(strategyId));
    }

    @GetMapping("/all")
    public ResponseEntity<List<TblPriceStrategy>> strategies(Boolean approved) {
        return ResponseEntity.ok(priceStrategyService.strategies(approved));
    }

    @GetMapping("/list")
    public ResponseEntity<PageResponse<TblPriceStrategy>>overview(PriceStrategyQueryParams params){
        return  ResponseEntity.ok(priceStrategyService.getPriceStrategies(params));
    }

    @GetMapping("/defaults")
    public ResponseEntity<List<TblPriceStrategy>> defaults() {
        return ResponseEntity.ok(priceStrategyService.defaultStrategies());
    }


    @PostMapping("/add")
    public ResponseEntity<Void> addPriceStrategy(@RequestBody @Valid PriceStrategyParams params) {
        priceStrategyService.addPriceStrategy(params);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/copy")
    public ResponseEntity<Void> copyPriceStrategy(@RequestBody @Valid PriceStrategyParams params) {
        priceStrategyService.copyPriceStrategy(params);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/approved/{strategyId}")
    public ResponseEntity<Void> approvedPriceStrategy(@PathVariable Long strategyId) {
        priceStrategyService.approvedPriceStrategy(strategyId);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public ResponseEntity<Void> updatePriceStrategy(@RequestBody @Valid PriceStrategyParams params) {
        if (Objects.isNull(params.getStrategyId())) {
            log.error("Strategy Id is required.");
            throw new ErpBusinessException(HttpStatus.BAD_REQUEST, ApplicationConstant.ILLEGAL_PARAMETER, "strategyId");
        }
        priceStrategyService.updatePriceStrategy(params);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/updateCategory")
    public ResponseEntity<List<String>> updatePriceStrategyCategoryCode() {
        return ResponseEntity.ok(priceStrategyService.updatePriceStrategyCategoryCode());
    }

    @PutMapping("/updateMultiPriceStrategy")
    public ResponseEntity<Void> updateMultiSetPriceStrategy(@RequestBody @Valid MultiPriceStrategyParams params) {
        priceStrategyService.updateMultiPriceStrategy(params);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/updateMultiMoqStrategy")
    public ResponseEntity<Void> updateMultiCountStrategy(@RequestBody @Valid MultiPriceStrategyParams params) {
        priceStrategyService.updateMultiMoqStrategy(params);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/updateMultiStopSellStrategy")
    public ResponseEntity<Void> updateMultiStopStrategy(@RequestBody @Valid MultiPriceStrategyParams params) {
        priceStrategyService.updateMultiStopSellStrategy(params);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{strategyId}")
    public ResponseEntity<Void> deletePriceStrategy(@PathVariable Long strategyId) {
        priceStrategyService.deletePriceStrategy(strategyId);
        return ResponseEntity.ok().build();
    }

}
